package com.example.bili.view.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
import android.widget.Toast;

import com.example.bili.R;
import com.example.bili.adapter.recommendedAdapter;
import com.example.bili.allbean.recommendedBean;
import com.example.bili.base.BaseFragment;
import com.example.bili.module.OkHttp;
import com.example.bili.util.GlideImageLoader;
import com.example.bili.view.activity.Webactivity;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Request;

import static com.example.bili.internet.connectors.RECOMMENDED;

/**
 * Created by Administrator on 2017/3/8. 推荐
 */

public class recommendedpage extends BaseFragment implements OkHttp.DataCallBack, OnBannerListener {
    private Banner banner;
    private List<String> bannerItemUrl;
    private ArrayList<String> bannerItemWebUrl;
    private recommendedBean recommendedbean;
    private RecyclerView recyclerview;

    @Override
    protected void initview(View view) {
        banner = (Banner) view.findViewById(R.id.d_rm_banner);
        recyclerview = (RecyclerView) view.findViewById(R.id.d_rm_recyclerview);

        bannerItemUrl = new ArrayList<>();
        bannerItemWebUrl = new ArrayList<>();
    }

    @Override
    protected void m_onCreateView() {
        //请求网络
        getinternerdata();
    }

    @Override
    public View getLayoutview() {
        return View.inflate(getActivity(), R.layout.recommendedpage, null);
    }

    public void getinternerdata() {
        //ok请求本类实现
        OkHttp.getAsync(RECOMMENDED, this);
    }

    @Override
    public void requestFailure(Request request, IOException e) {
        Toast.makeText(getActivity(), "网络请求失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void requestSuccess(String result) throws Exception {
        //Gson转换
        recommendedbean = new Gson().fromJson(result, recommendedBean.class);
        //筛选url
        getbannerurl();
    }
    private void getbannerurl() {
        //提取顶部轮播图片
        for (int i = 0; i < recommendedbean.getResult().size(); i++) {
            String type = recommendedbean.getResult().get(i).getType();
            if (type.equals("weblink")) {
                bannerItemUrl.add(recommendedbean.getResult().get(i).getBody().get(0).getCover());
                bannerItemWebUrl.add(recommendedbean.getResult().get(i).getBody().get(0).getParam());
            }
        }
        //初始化轮播图
        initbanner();

        ArrayList<recommendedBean.ResultBean> resultBean = new ArrayList<>();
        for (int i = 0; i < recommendedbean.getResult().size(); i++) {
            String style = recommendedbean.getResult().get(i).getHead().getStyle();
            if (style.equals("gm_av")) {
                resultBean.add(recommendedbean.getResult().get(i));
            }
        }
        //添加适配
        recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerview.setAdapter(new recommendedAdapter(getActivity(), resultBean));

    }
    private void initbanner() {
        //图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //条目点击事件
        banner.setOnBannerListener(this);
        //添加图片集合
        banner.setImages(bannerItemUrl);
        //开始轮播
        banner.start();
        //获取焦点
        banner.setFocusable(true);
        banner.setFocusableInTouchMode(true);
        banner.requestFocus();
    }
    @Override
    public void OnBannerClick(int position) {
        startActivity(new Intent(getActivity(), Webactivity.class).putExtra("weburl", bannerItemWebUrl.get(position)));
    }

}
